#pragma once
#include "session_link_player.h"
#include "tcp_network.h"
#include "player.h"
#include "singleton.h"
#include "match_server.h"
#include "game_conf.h"
#include "room.h"
#include <tuple>
#include <map>

class GameServer : public SessionLinkPlayer
{
protected:
	//用户所在的房间号 同时作为在线列表
	std::map<int, int> _uid_roomid_dict;
public:
	//TCP网络监听
	NetworkBase* _tcp_network;
	//
	GameConfig _config;
public:
	//房间分配类
	MatchServer<Room, 4> _rooms;
public:
	NetworkSessionBase * operator[](int fd);
	//用户离开某房间
	int PlayerLeaveRoom(int uid);
	//通过uid发送报文
	bool Send(int uid, const std::string& str);
	//路由分发
	int Dispatch(NetworkSessionBase* session);
	//根据UID获取所在的房间号码
	int GetRoom(int uid);
	//心跳
	int HeartBeat(NetworkSessionBase* session);
	//创建房间
	int CreateRoom(NetworkSessionBase* session);
	//加入房间
	int JoinInRoom(NetworkSessionBase* session);
	//玩家加入
	std::tuple<int,Player*> AddPlayer(NetworkSessionBase* session);
	//创建房间执行
	int HandleCreateRoom(NetworkSessionBase* session, Player* player);
	//加入房间执行
	int HandleJoinInRoom(NetworkSessionBase* session, Player* player);
	//发送错误信息
	void SendError(NetworkSessionBase * session, int error_code, const std::string& err_msg);
	//初始化游戏内容
	bool InitGameConfig();
	//解散开房房间
public:
	struct event_base * _base;
	//默认数据库
	std::string			_default_db;
	//服务器ID
	int					_server_id;
	//游戏信息
	std::string				_ip;
	int						_port;
	//websocket连接port
	int						_ws_port;
	//进场最小
	int						_min_access;
	//进场最大
	int						_max_access;
	//基础分
	int						_base_gold;
	//台费
	int						_tai_fee;
	//游戏类型
	int						_game_flag;
	//是否后台运行
	int						_is_daemonize;
	//配置文件目录
	std::string				_conf_file;
};

#define YXJMJ_SERVER (*Singleton<GameServer>::Instance())